
fname='C:\tmpdata\SL_110613_T100006';
[idata,imeta_data]=load_eye_monitor_data([fname '.eye'],1);
aux_data=load_lvd([fname '.lvd']);

pupil_diam=imeta_data(9,:);

iframe_times=get_iframe_times(imeta_data,aux_data(4,:));


stim_onsets=find(diff(round(aux_data(1,:))==4)==1);
base_onsets_raw=find(diff(round(aux_data(1,:))==2)==1);

base_onsets=[];
base_traces=[];
pupil_traces=[];

cnt=0;
for ind=1:length(base_onsets_raw)
    if isempty(intersect(stim_onsets,[base_onsets_raw(ind)-9000:base_onsets_raw(ind)-3000]))
        cnt=cnt+1;
        base_onsets(cnt)=base_onsets_raw(ind);
    end
end

cnt=0;
for ind=1:length(stim_onsets)
    [~,curr_frame]=min(abs(stim_onsets(ind)-iframe_times));
    try
        cnt=cnt+1;
    pupil_traces(cnt,:)=pupil_diam(curr_frame-50:curr_frame+300);
    end
end
   
cnt=0;
for ind=1:length(base_onsets)
    [~,curr_frame]=min(abs(base_onsets(ind)-iframe_times));
    try
        cnt=cnt+1;
    base_traces(cnt,:)=pupil_diam(curr_frame-50:curr_frame+300);
    end
end

figure;plot(mean(pupil_traces))
hold on
plot(mean(base_traces),'r')